package org.edu.modules.oa.controller;

import com.github.xiaoymin.knife4j.annotations.ApiSort;
import groovy.util.logging.Slf4j;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import org.apache.shiro.SecurityUtils;
import org.edu.common.api.vo.Result;
import org.edu.common.system.vo.LoginUser;
import org.edu.modules.oa.entity.VeOaMeetAnnUsersEntity;
import org.edu.modules.oa.entity.VeOaMeetAnnounceEntity;
import org.edu.modules.oa.entity.VeOaMeetUsersEntity;
import org.edu.modules.oa.service.IVeOaMeetAnnounceService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Api(tags = "会议管理---通知")
@RestController
@RequestMapping("oa/veOaMeetAnnounce")
@Slf4j
@ApiSort(value = 100)
public class VeOaMeetAnnounceController {
    @Resource
    private IVeOaMeetAnnounceService veOaMeetAnnounceService;
    @ApiModelProperty(value = "表格显示" , notes = "通知-----表格显示")
    @PostMapping("/showAnnounce")
    public Result<?> loadAnnounceTable(Integer start,Integer pageSize,String title,Integer meetId){
        HashMap<String,Object> map = veOaMeetAnnounceService.loadAnnounceTable(start,pageSize,title,meetId);
        return Result.OK(map);
    }

    @ApiModelProperty(value = "会议通知",notes = "通知------删除一条通知")
    @PostMapping("/deleteAnnounceOne")
    public Result<?> deleteAnnounceOne(Integer id){
        int res = veOaMeetAnnounceService.deleteAnnounceById(id);
        if(res == 0){
            return Result.error(500,"删除失败");
        }
        return Result.OK();
    }

    @ApiModelProperty(value = "会议通知", notes = "通知------删除多条通知")
    @PostMapping("/deleteAnnounceAll")
    public Result<?> deleteAnnounceAll(String ids){
        String[] arr = ids.split(",");
        for (String s : arr) {
            if(s != null && !s.equals("")){
                veOaMeetAnnounceService.deleteAnnounceById(Integer.parseInt(s));
            }
        }
            return Result.OK();
    }

    //查看的是参与会议人员表ve_oa_meet_users
    @ApiModelProperty(value = "会议通知", notes = "通知------查看参与会议的人员")
    @PostMapping("/selectMeetUsers")
    public Result<?> selectMeetUsers(Integer meetId){
        List<VeOaMeetUsersEntity> list = veOaMeetAnnounceService.selectMeetUsersById(meetId);
        return Result.OK(list);
    }

    //查看会议已通知的人员表ve_oa_meet_ann_users
    @ApiModelProperty(value = "会议通知", notes = "通知------查看参与会议的人员")
    @PostMapping("/selectMeetAnnUsers")
    public Result<?> selectMeetAnnUsers(Integer meetId,Integer anId){
        List<VeOaMeetAnnUsersEntity> list = veOaMeetAnnounceService.selectAnnUsersByMeetId(meetId,anId);
        return Result.OK(list);
    }

    //添加会议表和已通知的人员表添加两张表
    @ApiModelProperty(value = "会议通知", notes = "通知------查看参与会议的人员")
    @PostMapping("/addAnnounceAndUsers")
    public Result<?> addAnnounceAndUsers(VeOaMeetAnnounceEntity veOaMeetAnnounceEntity,String ids){

        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        veOaMeetAnnounceEntity.setCreateBy(user.getId());
        veOaMeetAnnounceEntity.setCreateName(user.getRealname());
        veOaMeetAnnounceEntity.setTerminalId(1L);
        System.out.println("==============");
        System.out.println(ids);
        System.out.println(veOaMeetAnnounceEntity);
        int res = veOaMeetAnnounceService.insertMeetUserAnno(veOaMeetAnnounceEntity,ids);
        if(res == 0){
            return Result.error(500,"添加失败了");
        }
        return Result.OK();
    }

    //修改会议表和已通知的人员两张一起修改
    @ApiModelProperty(value = "会议通知", notes = "通知------修改通知会议人员表")
    @PostMapping("/setAnnounceAndUsers")
    public Result<?> setAnnounceAndUsers(VeOaMeetAnnounceEntity veOaMeetAnnounceEntity,String ids){
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        veOaMeetAnnounceEntity.setUpdateBy(user.getId());
        veOaMeetAnnounceEntity.setTerminalId(1L);
        System.out.println("==============");
        System.out.println(ids);
        System.out.println(veOaMeetAnnounceEntity);
        int res = veOaMeetAnnounceService.updateMeetUserAnno(veOaMeetAnnounceEntity,ids);
        if(res == 0){
            return Result.error(500,"修改失败了");
        }
        return Result.OK();
    }



}
